package com.jack.module_compose

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.VerticalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.unit.dp
import cn.jack.lib_base.base.view.BaseComposeActivity
import cn.jack.library_arouter.manager.constants.RouterPathActivity
import coil.compose.rememberImagePainter
import com.alibaba.android.arouter.facade.annotation.Route
import kotlinx.coroutines.delay
import kotlin.math.absoluteValue

/**
 * @创建者 Jack
 * @创建时间 2024-09-20 23:11
 * @描述
 */
@Route(path = RouterPathActivity.ComposeTest.PAGER_COMPOSE_TEST)
class TestComposeActivity : BaseComposeActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TestUI()
        }
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun TestUI() {
//    Text(text = "Compose")

    HorizontalPager(pageCount = 10) {
//        page ->
        Card(
            Modifier
                .fillMaxWidth()
                .height(200.dp)
                .graphicsLayer {
                    // 计算当前页相对于滚动位置
//                    val pageOffset = (
//                            (pagerState.currentPage - page) + pagerState.currentPageOffsetFraction
//                            ).absoluteValue
//
////                     动画 alpha
//                    alpha = lerp(
//                        start = 0.5f,
//                        stop = 1f,
//                        fraction = 1f - pageOffset.coerceIn(0f, 1f)
//                    )
//
//                    // 翻转效果
//                    rotationY = if (pageOffset < 1f) {
//                        (pageOffset * 180).coerceIn(0f, 180f)
//                    } else {
//                        180f
//                    }
                }
        ) {
            Image(
                painter = rememberImagePainter("https://wanandroid.com/blogimgs/8a0131ac-05b7-4b6c-a8d0-f438678834ba.png"),
                contentDescription = "",
                modifier = Modifier.fillMaxSize()
            )
        }
    }
}

//@OptIn(ExperimentalFoundationApi::class)
//@Composable
//fun Carousel(banners: List<BannerBean>) {
//    val pagerState = rememberPagerState(initialPage = 0, pageCount = { banners.size })
//
//    HorizontalPager(state = pagerState) { page ->
//        Card(
//            Modifier
//                .fillMaxWidth()
//                .height(200.dp)
//                .graphicsLayer {
//                    // 计算当前页相对于滚动位置
//                    val pageOffset = (
//                            (pagerState.currentPage - page) + pagerState.currentPageOffsetFraction
//                            ).absoluteValue
//
//                    // 动画 alpha
//                    alpha = lerp(
//                        start = 0.5f,
//                        stop = 1f,
//                        fraction = 1f - pageOffset.coerceIn(0f, 1f)
//                    )
//
//                    // 翻转效果
//                    rotationY = if (pageOffset < 1f) {
//                        (pageOffset * 180).coerceIn(0f, 180f)
//                    } else {
//                        180f
//                    }
//                }
//        ){
//            Image(
//                painter = rememberImagePainter(banners[page].imagePath),
//                contentDescription = "",
//                modifier = Modifier.fillMaxSize()
//            )
//        }
//    }
//
//    LaunchedEffect(Unit) {
//        while (true) {
//            delay(2000) // 每3秒切换一次
//            val nextPage = (pagerState.currentPage + 1) % banners.size
//            pagerState.animateScrollToPage(nextPage)
//        }
//    }
//}